package com.xydtech.ecard.merchant.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.merchant.domain.EcardPersonArea;
import com.xydtech.ecard.merchant.domain.EcardPersonAreaHandover;
import com.xydtech.ecard.merchant.mapper.EcardPersonAreaHandoverMapper;
import com.xydtech.ecard.merchant.mapper.EcardPersonAreaMapper;
import com.xydtech.ecard.merchant.service.IEcardPersonAreaService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 电子名片个人网格区域信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-09-14 上午10:29
 */
@Service
public class EcardPersonAreaServiceImpl implements IEcardPersonAreaService {

    @Autowired
    private EcardPersonAreaMapper ecardPersonAreaMapper;
    @Autowired
    private EcardPersonAreaHandoverMapper ecardPersonAreaHandoverMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询电子名片个人网格区域信息
     *
     * @param id 电子名片个人网格区域信息主键
     * @return 电子名片个人网格区域信息
     */
    @Override
    public EcardPersonArea selectEcardPersonAreaById(Long id, String salt)
    {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        EcardPersonArea personArea = ecardPersonAreaMapper.selectEcardPersonAreaById(id, sysDept.getDeptId());
        if (personArea != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(personArea.getAreaPhone())) {
                    personArea.setAreaPhone(AESEncryptionUtils.decrypt(personArea.getAreaPhone(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return personArea;
    }

    /**
     * 查询电子名片个人网格区域信息列表
     *
     * @param ecardPersonArea 电子名片个人网格区域信息
     * @return 电子名片个人网格区域信息
     */
    @Override
    @DataScope(userAlias = "c", deptAlias = "b")
    public List<EcardPersonArea> selectEcardPersonAreaList(EcardPersonArea ecardPersonArea, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardPersonArea.getAreaPhone())) {
                ecardPersonArea.setAreaPhone(AESEncryptionUtils.encrypt(ecardPersonArea.getAreaPhone(), salt));
            }
            List<EcardPersonArea> ecardPersonAreaList = ecardPersonAreaMapper.selectEcardPersonAreaList(ecardPersonArea);
            if (ecardPersonAreaList.size() > 0) {
                for (EcardPersonArea personArea : ecardPersonAreaList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(personArea.getAreaPhone())) {
                        personArea.setAreaPhone(AESEncryptionUtils.decrypt(personArea.getAreaPhone(), salt));
                    }
                }
            }
            return ecardPersonAreaList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 批量移交网格信息
     * @param ecardPersonAreaList
     * @param registId
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int allocationEcardPersonAreaInfo(List<EcardPersonArea> ecardPersonAreaList, Long registId, Long userId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        //转交记录
        List<EcardPersonAreaHandover> ecardPersonAreaHandoverList = new ArrayList<>();
        for(int i = 0; i < ecardPersonAreaList.size(); i++){
            EcardPersonArea ecardPersonArea = ecardPersonAreaList.get(i);
            //查询网格信息
            EcardPersonArea oldEcardPersonAreaInfo = ecardPersonAreaMapper.selectEcardPersonAreaById(ecardPersonArea.getId(), sysDept.getDeptId());

            EcardPersonAreaHandover ecardPersonAreaHandover = new EcardPersonAreaHandover();
            ecardPersonAreaHandover.setAreaId(ecardPersonArea.getId());
            ecardPersonAreaHandover.setTransferId(oldEcardPersonAreaInfo.getManagementPersonnelId());
            ecardPersonAreaHandover.setTransferDeptId(oldEcardPersonAreaInfo.getDeptId());
            ecardPersonAreaHandover.setRecipientId(ecardPersonArea.getManagementPersonnelId());
            ecardPersonAreaHandover.setRecipientDeptId(ecardPersonArea.getDeptId());
            ecardPersonAreaHandoverList.add(ecardPersonAreaHandover);
        }

        //批量移交网格
        int a = ecardPersonAreaMapper.batchUpdateEcardPersonAreaInfo(ecardPersonAreaList);
        //批量插入移交记录
        int b = ecardPersonAreaHandoverMapper.batchInsertEcardAreaHandover(ecardPersonAreaHandoverList);

        return a > b ? b:a;
    }

    /**
     * 批量启用/停用网格
     * @param ecardPersonAreaList
     * @return
     */
    @Override
    public int batchSwitchOverEcardPersonAreaInfo(List<EcardPersonArea> ecardPersonAreaList) {
        return ecardPersonAreaMapper.batchSwitchOverEcardPersonAreaInfo(ecardPersonAreaList);
    }
}
